﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLToWordLib
{
    public class DBLinkPool
    {
        List<DBLinkUser> LinkPool;
        public  DBLinkPool()
        {
            LinkPool = new List<DBLinkUser>();
        }
        public void Add(string id, string name, string connectionString)
        {
            int selCount=0;
            if(LinkPool !=null)
            {
                 var selectObj = from LinkUser in LinkPool
                                 where LinkUser.Id == id
                                     select LinkUser;
                selCount=selectObj.Count<DBLinkUser>();
            }
            if(selCount < 1)
            {
                DBLinkUser dbLinkUser = new DBLinkUser();
                dbLinkUser.Id = id;
                dbLinkUser.Name = name;
                dbLinkUser.ConnectionString = connectionString;
                LinkPool.Add(dbLinkUser);            
            }            
        }

        public DBLinkUser GetLinkUser(string id)
        {

            if (LinkPool == null) return null;
            int selCount = 0;  
            var selectObj = (from LinkUser in LinkPool
                                where LinkUser.Id == id
                                select LinkUser).First<DBLinkUser>();
            if (selectObj == null)
            {

                return null;
            }
            else
            {
                return (DBLinkUser)selectObj;
            }
        }

        /// <summary>
        /// 释放数据库连接
        /// </summary>
        ~DBLinkPool()
        {
            foreach (DBLinkUser linkUser in LinkPool)
            {
                if (linkUser != null)
                { 
                        linkUser.Close();                 
                }                
            }
        
        }



    }
}
